package cn.good.biz.controller;


import cn.good.biz.service.GDArticleService;
import cn.good.dto.Dto;
import cn.good.dto.DtoUtil;
import cn.good.pojo.Article;
import cn.good.vo.ArticleVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/doc/article")
public class ArticleController {

    @Resource
    private GDArticleService atricleService;

    @ApiOperation(value="查询文章列表",httpMethod = "POST",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class,notes="查询文章列表")
    @RequestMapping("/articlelist")
    public Dto fingArticleList(@RequestBody ArticleVO vo){
        Map<String,Object> param=new HashMap<String,Object>();
        if(vo.getType1()!=null&&!vo.getType1().equals("")){
            param.put("type1",vo.getType1());
            if(vo.getType2()!=null&&!vo.getType2().equals("")){
                param.put("type2",vo.getType2());
            }
        }
        List<Article> list=null;
        try {
            list=atricleService.findArticleList(param);
            return DtoUtil.returnDataSuccess(list);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("获取文章列表失败","20014");
        }

    }

    @ApiOperation(value="查询文章详情",httpMethod = "POST",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class,notes="查询文章详情")
    @RequestMapping("/article")
    public Dto findArticle(String articleId){
        List<Article> articleList=null;
        if(articleId!=null) {
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("id", articleId);
            try {
                articleList = atricleService.findArticleList(param);
                return DtoUtil.returnDataSuccess(articleList.get(0));
            } catch (Exception e) {
                e.printStackTrace();
                return DtoUtil.returnFail("获取文章详情失败","20012");
            }
        }
        return DtoUtil.returnFail("文章id不能为空","20013");
    }


}
